package com.qingcheng.controller.file;
import com.aliyun.oss.OSSClient;
import com.qingcheng.controller.untilAndConfigNotBelongToProject.FastDFSClient;
import com.qingcheng.controller.untilAndConfigNotBelongToProject.FastDFSFile;
import org.csource.common.MyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.UUID;

@RestController
@RequestMapping("/upload")
public class UploadController {


    @Autowired
    private HttpServletRequest request;

    @PostMapping("/native")
    public String nativeUpload(@RequestParam("file") MultipartFile file){

        String path = request.getSession().getServletContext().getRealPath("img");
        String filePath = path+"/"+file.getOriginalFilename();
        File desFile = new File(filePath);
        if(!desFile.getParentFile().exists()){
            desFile.mkdirs();
        }
        try {
            file.transferTo(desFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "http://localhost:9101/img/"+file.getOriginalFilename();
    }

    @Autowired
    private OSSClient ossClient;

    /**
     * 上传到阿里oss储存库
     * @param file
     * @param folder
     * @return
     */
    @PostMapping("/oss")
    public String ossUpload(@RequestParam("file") MultipartFile file,String folder){
        String bucketName = "qingcheng120";
        String fileName = folder+"/"+UUID.randomUUID()+file.getOriginalFilename();
        try {
            //上传到oss中
            ossClient.putObject(bucketName,fileName, file.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "https://"+bucketName+".oss-cn-shanghai.aliyuncs.com/"+fileName;
    }

    /**
     * 自己写的fastDFS测试，不在服务之内，本项目用的阿里oss
     * @param file
     * @return
     */
    @PostMapping("/fastDFSUpload")
    public String fastDFSUpload(@RequestParam("file") MultipartFile file,String folder) throws IOException, MyException {
        //封装文件信息
        FastDFSFile fastDFSFile = new FastDFSFile(
                file.getOriginalFilename(), //文件名字 1.jpg
                file.getBytes(), //文件字节数组
                StringUtils.getFilenameExtension(file.getOriginalFilename()) //获取文件扩展名
        );
        fastDFSFile.setName(folder+"/"+fastDFSFile.getName());
//        System.out.println("name"+fastDFSFile.getName());

        //调用FastDFSUtil工具类将文件传入到FastDFS中
        String[] uploads =  FastDFSClient.upload(fastDFSFile);
        //拼接访问地址 url=http://192.168.211.135:8080/group1/M00/00/00/DSAFA.JPG
        //String url = "http://192.168.211.135:8080/"+uploads[0]+"/"+uploads[1];
        String url = FastDFSClient.getTrackerInfo()+"/"+uploads[0]+"/"+uploads[1];
//        System.out.println(url+"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
        //返回图片地址
        return url;
    }

}
